package com.github.data_manage.dao

import com.github.data_manage.domain.po.FundDepthResearchContrastAttach
import com.github.data_manage.domain.po.FundNavData
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import java.time.LocalDate

/**
 *
 * @author chenhainan
 * @since 2025/8/13
 */
interface FundDepthResearchContrastAttachDao : BaseDao<FundDepthResearchContrastAttach> {

    fun findByFundIdEqualsAndContrastIdEqualsAndDataMonthEquals(
        fundId: Int,
        contrastId: Int,
        dataMonth: LocalDate
    ): FundDepthResearchContrastAttach?

    fun findAllByFundIdIsAndContrastIdIn(fundId: Int, contrastIds: List<Int>): List<FundDepthResearchContrastAttach>

    @Query("select f from FundDepthResearchContrastAttach f where f.fundId=:fundId and f.contrastId=:contrastId")
    fun findAllByFundIdIsAndContrastIdIs(
        @Param("fundId") fundId: Int,
        @Param("contrastId") contrastId: Int
    ): List<FundDepthResearchContrastAttach>

    fun findAllByFundIdIsAndContrastIdIn(
        fundId: Int, contrastId: MutableCollection<Int>
    ): List<FundDepthResearchContrastAttach>

    @Query("select f from FundDepthResearchContrastAttach f where f.fundId=:fundId")
    fun findAllByFundId(
        @Param("fundId") fundId: Int
    ): List<FundDepthResearchContrastAttach>

    @Query("""
        select f from FundDepthResearchContrastAttach f 
        where f.fundId = :fundId and f.contrastId = :contrastId 
        and f.dataMonth < :lastMonth order by f.dataMonth desc limit 1
    """)
    fun findLastDataByFundId(@Param("fundId") fundId: Int,
                             @Param("contrastId") contrastId: Int,
                             @Param("lastMonth") lastMonth: LocalDate): FundDepthResearchContrastAttach?
}